package lanou.autohome.recommend.fragments;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Timer;

import lanou.autohome.R;
import lanou.autohome.URLValues;
import lanou.autohome.basetool.BaseFragment;
import lanou.autohome.basetool.NetTool;
import lanou.autohome.carouselpic.CarouselAdapter;
import lanou.autohome.recommend.adapters.GachincoAdapter;
import lanou.autohome.recommend.beans.GachincoBean;

/**
 * Created by dllo on 16/8/15.
 * 推荐 -> 最新
 */
public class GachincoFragment extends BaseFragment {

    private GachincoAdapter gachincoAdapter;
    private ListView listView;
    private Handler handler;
    private ArrayList<String> strings;
    private ViewPager viewPager;
    private View vpview;
    //轮播图小点集合
    private ImageView[] islarge;
    private LinearLayout linearLayout;
    //判断是否需要轮播标志
    private boolean isContinue = true;
    //定时器，用于实现轮播
    private Timer timer = new Timer();
    //当前索引位置以及上一个索引位置
    private static int index = 0, preIndex = 0;


    @Override
    protected int setLayout() {
        return R.layout.fragment_recommend_gachinco;
    }

    @Override
    protected void initView() {
        listView = bindVie(R.id.listView_recommend_gachonco);

        vpview = LayoutInflater.from(getContext()).inflate(R.layout.fragment_recommend_gachinco_headerview,null);
        viewPager = bindView(vpview,R.id.viewpager_recommend_gachinco_header);

        linearLayout = (LinearLayout) vpview.findViewById(R.id.relativelayout_recommend_gachinco_headerview);
    }

    @Override
    protected void intiData() {
        gachincoAdapter = new GachincoAdapter(mcontext);
        listView.setAdapter(gachincoAdapter);
        mNetTool.getNetData(URLValues.NEW_URL, GachincoBean.class, new NetTool.NetListener<GachincoBean>() {
            @Override
            public void onSuccess(GachincoBean gachincoBean) {
                gachincoAdapter.setGachincoBean(gachincoBean);
            }
            @Override
            public void onError(String errorMsg){
            }
        });

        //轮播图
        final CarouselAdapter carouselAdapter = new CarouselAdapter(getContext());
        strings = new ArrayList<>();
        mNetTool.getNetData(URLValues.NEW_URL, GachincoBean.class, new NetTool.NetListener<GachincoBean>() {
            @Override
            public void onSuccess(final GachincoBean gachincoBean) {

                for (int i = 0; i < gachincoBean.getResult().getFocusimg().size(); i++) {
                    strings.add(gachincoBean.getResult().getFocusimg().get(i).getImgurl());
                }
                carouselAdapter.setStrings(strings);
               // carouselAdapter.setViewPager(viewPager);
                viewPager.setAdapter(carouselAdapter);
                listView.addHeaderView(vpview);

                //为轮播图添加可点击事件
                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        for (int i = 0; i < islarge.length; i++) {
                            if(i == position % gachincoBean.getResult().getFocusimg().size()){
                                islarge[i].setImageResource(R.drawable.shape_point_white_carousel);
                            }else {
                                islarge[i].setImageResource(R.drawable.shape_point_gray_carousel);
                            }
                        }
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
                islarge = new ImageView[gachincoBean.getResult().getFocusimg().size()];
                for (int i = 0; i < gachincoBean.getResult().getFocusimg().size(); i++) {
                    Log.d("GachincoFragment", i + "");
                    ImageView imageView = new ImageView(mcontext);
                    imageView.setLayoutParams(new LinearLayout.LayoutParams(20 , 20));
                    islarge[i] = imageView;
                    if (i == 0) {
                        imageView.setImageResource(R.drawable.shape_point_white_carousel);

                    } else {
                        imageView.setImageResource(R.drawable.shape_point_gray_carousel);
                    }
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20 , 20);
                    layoutParams.rightMargin = 20;
                    layoutParams.leftMargin = 20;
                    linearLayout.addView(imageView,layoutParams);
                }


            }

            @Override
            public void onError(String errorMsg) {
                Toast.makeText(mcontext, "请打开您的网络", Toast.LENGTH_SHORT).show();
            }
        });

        //前边有3000页
        viewPager.setCurrentItem(3000, false);


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.sendEmptyMessage(viewPager.getCurrentItem());
                }
            }
        }).start();

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {

                int current = msg.what;
                viewPager.setCurrentItem(current + 1);

                return false;
            }
        });
    }

    public void onTouchViewPager(View view , final int position){
        //根据当前触摸事件判断是否需要轮播
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    //手按下和滑动的时候停止图片的轮播
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        isContinue = false;
                    default:
                        isContinue = true;
                }
                //这里只能返回false , 如果返回ture , Down就会消费掉事件 , move无法获得事件
                //导致图片无法滑动
                return false;
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //页面销毁的时候取消定时器
        if(timer != null){
            preIndex = 0;
            index = 0;
            timer.cancel();
        }
    }
}
